﻿using System;
using System.Threading;

namespace Skynet.CommonLibrary.ThreadRefreshTime.StrateGy
{
    /// <summary>
    /// 线程
    /// </summary>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-03-28
    /// </remarks>
    public class ThreadStrategy : IThreadStrategy
    {
        Thread _currentThread;
        #region IThreadSerategy 成员

        public void StartWork(Action workAction,int interval)
        {
            if (workAction == null)
                return;

            //自定义线程
            _currentThread = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    workAction();
                    Thread.Sleep(interval);
                }
            }));
            _currentThread.IsBackground = true;
            _currentThread.Start();
        }

        public void StopWork()
        {
            if (_currentThread != null)
            {
                try
                {
                    if (_currentThread.IsAlive)
                        Thread.Sleep(1);

                    _currentThread.Abort();
                    _currentThread.Join();
                }
                catch { }
            }
        }

        #endregion
    }
}
